{include file="public/header" /}
<span class="layui-breadcrumb">
  <a href="{:url('Blog/index')}?nav=blog-bloglist">分类</a>
  <a><cite>编辑分类</cite></a>
</span>
<style> 
.layui-form-label{width:200px;}
.layui-form-item .layui-input-block{margin-left:230px;}
</style>
<div class="layui-fluid">
    <div class="layui-card">
    	<form class="layui-form" id="product_form" action="" style='padding:10px'>
        <div style="color:#FF5722">只能同等级下和当前等级之上的目录更改</div>
        <div class="layui-form-item">
          <label class="layui-form-label">所属父类</label>
          {foreach $fl as $key=>$vo } 
          <div class="layui-input-block" style="margin-top:10px;display: block;">
            <select name="quiz<?php echo count($fl)-($key+1);?>" id='quiz<?php echo count($fl)-($key+1);?>' lay-filter="sel<?php echo count($fl)-($key+1);?>">
              <option value="0">请选择分类</option>
              {foreach $vo['all'] as $k=>$v }
              <option value="{$v['id']}" {eq name="$v['id']" value="$vo['xz']['id']"}selected{/eq}>{$v['cname']}</option>
              {/foreach}
            </select>
          </div>
          {/foreach} 

        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">分类名称</label>
          <div class="layui-input-block">
              <input type="text" name="cname" value="{$one['cname']}" autocomplete="off" placeholder="请输入分类名称" class="layui-input">
          </div>
        </div>
         <div class="layui-form-item">
          <label class="layui-form-label">上传图片</label>
          <div class="layui-input-block">
            <div class="layui-upload">
            <button type="button" class="layui-btn layui-btn-xs" id="upload_img">选择图片</button>
            <div class="layui-upload-list">
              <img class="layui-upload-img" style='width:200px' id="show_img" <?php if(!empty($one['img'])){?>src="{$one['img']}"<?php } ?>>
              <p id="demoText"></p>
            </div>
          </div> 
          </div>
          <input type="text" name="newname" style="display:none" <?php if(!empty($one['img'])){?>value="{$one['img']}"<?php } ?>>
        </div>

        <div class="layui-form-item">
          <label class="layui-form-label"></label>
          <div class="layui-input-block">
             <button type='button' class="layui-btn layui-btn-normal" onclick="tijiao({$one['id']})">确定</button>
          </div>
        </div>

		</form>
    </div>
</div>
{include file="/public/footer" /}
<script>
layui.use(['upload','form'], function(){
  var $ = layui.jquery,upload = layui.upload;
  var form = layui.form

  //普通图片上传
  var uploadInst = upload.render({
    elem: '#upload_img'
    ,accept: 'file'
    ,url: "{:url('Base/upload')}"+"?upload_url=static/upload/classify/" //改成您自己的上传接口
    ,before: function(obj){
      //预读本地文件示例，不支持ie8
      obj.preview(function(index, file, result){
        $('#show_img').attr('src', result); //图片链接（base64）
      });
    }
    ,done: function(res){
      //如果上传失败
      if(res.status == '0'){
        return layer.msg(res.msg);
      }else{ //上传成功
        $("input[name='newname']").val(res.newname);
        return layer.msg(res.msg);
      }
    }
    ,error: function(){
      //演示失败状态，并实现重传
      var demoText = $('#demoText');
      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
      demoText.find('.demo-reload').on('click', function(){
        uploadInst.upload();
      });
    }
  });


  /*联动1*/
  form.on('select(sel0)', function(data){   
      var val=data.value;
      if(val=='0'){
        $("#quiz1").parent().css('display','none');
        $("#quiz1").html('');
        $("#quiz2").parent().css('display','none');
        $("#quiz2").html('');
        form.render('select');
        return false;
      }
      $.post("{:url('Classify/sel')}",{val:val},function(res){
          $("#quiz1").html(res);
          $("#quiz1").parent().css('display','block');
          form.render('select');
      })
  });
  /*联动2*/
  form.on('select(sel1)', function(data){   
      var val=data.value;
      if(val=='0'){
        $("#quiz2").parent().css('display','none');
        $("#quiz2").html('');
        form.render('select');
        return false;
      }
      $.post("{:url('Classify/sel')}",{val:val},function(res){
          $("#quiz2").html(res);
          $("#quiz2").parent().css('display','block');
          form.render('select');
      })
  });
  
 
}); 

function tijiao(id){
  layer.load();
  var cname=$.trim($("input[name='cname']").val());
  if(cname==''){
    layer.closeAll('loading');
    $("input[name='cname']").focus();
    layer.msg('请填写分类名称');
    return false;
  }
  var quiz0=$.trim($("select[name='quiz0'] option:selected").val());
  var quiz1=$.trim($("select[name='quiz1'] option:selected").val());
  var quiz2=$.trim($("select[name='quiz2'] option:selected").val());
  var newname=$.trim($("input[name='newname']").val());
  /*在这里进行数据分析确定父id为多少 等级为多少 若quiz1为0则fid为0 level为1,若quiz1不为0*/
  if(quiz0==''||quiz0=='0'){
    var fid=0;
    var level=1;
  }else{
    if(quiz1==''||quiz1=='0'){
      var fid=quiz0;
      var level=2;
    }else{
       if(quiz2==''||quiz2=='0'){
        var fid=quiz1;
        var level=3;
      }else{
        var fid=quiz2;
        var level=4;
      }
    }
  }

  $.post("{:url('Classify/edit')}",{id:id,cname:cname,fid:fid,level:level,newname:newname},function(res){
    if(res.status=='0'){
      layer.closeAll('loading');
      layer.msg(res.msg);
    }
    if(res.status=='1'){
      layer.msg(
      res.msg,{time: 1500 },
      function(){
        layer.closeAll();
        location.href=location.href;
      });
    }
  })
}

</script>
